package com.example.mobile2030606150cjk0325;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private EditText edtTxtMainShow;
    private Button btnMainBack;
    private Button btnMainMul;
    private Button btnMainDiv;
    private Button btnMainClc;
    private Button btnMainNum7;
    private Button btnMainNum8;
    private Button btnMainNum9;
    private Button btnMainSub;
    private Button btnMainNum4;
    private Button btnMainNum5;
    private Button btnMainNum6;
    private Button btnMainPersent;
    private Button btnMainNum1;
    private Button btnMainNum2;
    private Button btnMainNum3;
    private Button btnMainAdd;
    private Button btnMainNum0;
    private Button btnMainPoint;
    private Button btnMainEqu;
    boolean clear_flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainNum0.setOnClickListener(this);
        btnMainNum1.setOnClickListener(this);
        btnMainNum2.setOnClickListener(this);
        btnMainNum3.setOnClickListener(this);
        btnMainNum4.setOnClickListener(this);
        btnMainNum5.setOnClickListener(this);
        btnMainNum6.setOnClickListener(this);
        btnMainNum7.setOnClickListener(this);
        btnMainNum8.setOnClickListener(this);
        btnMainNum9.setOnClickListener(this);
        btnMainClc.setOnClickListener(this);
        btnMainBack.setOnClickListener(this);
        btnMainAdd.setOnClickListener(this);
        btnMainEqu.setOnClickListener(this);
        btnMainMul.setOnClickListener(this);
        btnMainPersent.setOnClickListener(this);
        btnMainPoint.setOnClickListener(this);
        btnMainSub.setOnClickListener(this);

    }

    private void initView() {
        edtTxtMainShow = (EditText) findViewById(R.id.edtTxt_main_show);
        btnMainNum0 = (Button) findViewById(R.id.btn_main_num0);
        btnMainNum7 = (Button) findViewById(R.id.btn_main_num7);
        btnMainNum8 = (Button) findViewById(R.id.btn_main_num8);
        btnMainNum9 = (Button) findViewById(R.id.btn_main_num9);
        btnMainNum4 = (Button) findViewById(R.id.btn_main_num4);
        btnMainNum5 = (Button) findViewById(R.id.btn_main_num5);
        btnMainNum6 = (Button) findViewById(R.id.btn_main_num6);
        btnMainNum1 = (Button) findViewById(R.id.btn_main_num1);
        btnMainNum2 = (Button) findViewById(R.id.btn_main_num2);
        btnMainNum3 = (Button) findViewById(R.id.btn_main_num3);


        btnMainAdd = (Button) findViewById(R.id.btn_main_add);//+
        btnMainPoint = (Button) findViewById(R.id.btn_main_point);//.
        btnMainEqu = (Button) findViewById(R.id.btn_main_equ);//=
        btnMainBack = (Button) findViewById(R.id.btn_main_back); //清除
        btnMainDiv = (Button) findViewById(R.id.btn_main_div);//*
        btnMainClc = (Button) findViewById(R.id.btn_main_clc);///
        btnMainMul = (Button) findViewById(R.id.btn_main_mul); //退格
        btnMainPersent = (Button) findViewById(R.id.btn_main_persent);//%
        btnMainSub = (Button) findViewById(R.id.btn_main_sub);//-


    }


    @Override
    public void onClick(View v) {
        String str =edtTxtMainShow.getText().toString();
        switch(v.getId ()){
            case R.id.btn_main_num0:
            case R.id.btn_main_num1:
            case R.id.btn_main_num2:
            case R.id.btn_main_num3:
            case R.id.btn_main_num4:
            case R.id.btn_main_num5:
            case R.id.btn_main_num6:
            case R.id.btn_main_num7:
            case R.id.btn_main_num8:
            case R.id.btn_main_num9:
            case R.id.btn_main_point:
                if(clear_flag){
                    clear_flag = false;
                    edtTxtMainShow.setText("");//赋值为空
                }
                edtTxtMainShow.setText(str + ((Button)v).getText());//结果集就为本身
                break;


            case R.id.btn_main_add:
            case R.id.btn_main_sub:
            case R.id.btn_main_div:
            case R.id.btn_main_clc:
                if(clear_flag){
                    clear_flag=false;
                    str = "";
                    edtTxtMainShow.setText("");
                }
                edtTxtMainShow.setText(str+" "+((Button)v).getText()+" ");
                break;

            case R.id.btn_main_mul:
                if(clear_flag){
                    clear_flag=false;
                    edtTxtMainShow.setText ("");
                }else if (str != null && !str.equals ("")){
                    edtTxtMainShow.setText(str.substring(0,str.length()-1));    //删除一个字符
                }
                break;

            case R.id.btn_main_back:
                clear_flag=false;
                str = "";
                edtTxtMainShow.setText("");        //清空文本内容
                break;

            case R.id.btn_main_equ:
                getResult();              //获取结果
                break;
            default:
                break;
        }
    }
    //算法
    private void getResult() {
        String xiao = edtTxtMainShow.getText().toString();
        if (xiao == null || xiao.equals("")) {
            return;
        }
        if (!xiao.contains("")) {
            return;
        }
        if (clear_flag) {
            clear_flag = false;
            return;
        }
        clear_flag = true;

        String str1 = xiao.substring(0, xiao.indexOf(" "));                      // 获取到运算符前面的字符
        String str_y = xiao.substring(xiao.indexOf(" ") + 1, xiao.indexOf(" ") + 2);    //获取到运算符
        String str2 = xiao.substring(xiao.indexOf(" ") + 3);                     //获取到运算符后面的字符

        double result = 0;
        if (!str1.equals("") && !str2.equals("")) {
            double num1 = Double.parseDouble(str1);   //将str1、str2强制转化为double类型
            double num2 = Double.parseDouble(str2);

            if (str_y.equals("+")) {
                result = num1 + num2;
            } else if (str_y.equals("-")) {
                result = num1 - num2;
            } else if (str_y.equals("/")) {
                if (num2 == 0) {
                    result = 0;
                } else {
                    result = num1 / num2;
                }
            } else if (str_y.equals("*")) {
                result = num1 * num2;
            }
            if (!str1.contains(".") && !str2.contains(".") && !xiao.equals("÷")) {
                int k = (int) result;               //强制转换
                edtTxtMainShow.setText(k + "");
            } else {
                edtTxtMainShow.setText(result + "");
            }
        } else if (!str1.equals("") && str2.equals("")) {
            edtTxtMainShow.setText(xiao);
        } else if (str1.equals("") && !str2.equals("")) {
            double num2 = Double.parseDouble(str2);
            if (xiao.equals("+")) {
                result = 0 + num2;
            } else if (xiao.equals("-")) {
                result = 0 - num2;
            } else if (xiao.equals("×")) {
                result = 0;
            } else if (xiao.equals("/")) {
                result = 0;
            }
            if (!str2.contains(".")) {
                int r = (int) result;
                edtTxtMainShow.setText(r + "");
            } else {
                edtTxtMainShow.setText(result + "");
            }
        } else {
            edtTxtMainShow.setText("");
        }
    }


}